<template>
  <div id="Company_drugs_add">
    <span>
      <img src="../assets/medicine.png" alt="" class="bread-icon">
    </span>
    <el-breadcrumb style="display:inline-block">
      <el-breadcrumb-item>
        <span style="font-size:20px;">药品管理</span>
      </el-breadcrumb-item>
      <el-breadcrumb-item>
        <span style="font-size:15px;line-height:20px;">药品添加</span>
      </el-breadcrumb-item>
    </el-breadcrumb>
    <div class="content-wrap">
      <div class="topNav">
        <span class="navLeft">药品添加</span>
        <!-- <span class="navRight">
          <el-button size="small" @click="dragSearch()">搜索</el-button>
        </span>
        <span class="navMid">
          <img class="icon-search" src="../assets/search.png" alt="">
          <el-input v-model="searchData" size="small" placeholder="请输入内容" class="searchInput"></el-input>
        </span> -->
      </div>
      <el-form :model="formData">
        <el-form-item label="药品通用名称" :label-width="formLabelWidth">
          <el-input v-model="formData.common_name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="批准文号" :label-width="formLabelWidth">
          <el-input v-model="formData.approval_number" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="批准文号有效期" :label-width="formLabelWidth">
          <el-input v-model="formData.approval_expiry_date" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="药品有效期" :label-width="formLabelWidth">
          <el-input v-model="formData.expiry_date_number" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="有效期单位" :label-width="formLabelWidth">
          <el-input v-model="formData.expiry_date_unit" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="剂型" :label-width="formLabelWidth">
          <el-input v-model="formData.form" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="制剂规格" :label-width="formLabelWidth">
          <el-input v-model="formData.standard" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="生产企业" :label-width="formLabelWidth">
          <el-input v-model="formData.factory" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="所属企业 *" :label-width="formLabelWidth">
          <el-input v-model="formData.enterprise" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div class="btn-group">
        <el-button>取消</el-button>
        <el-button type="primary" @click="createDrug">添加</el-button>
      </div>
    </div>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        dialogTableVisible: false,
        dialogFormVisible: false,
        adminId: '',
        adminToken: '',
        formData: {},
        formLabelWidth: '130px'
      }
    },
    methods: {
      createDrug(row) {
        this.$http({
          url: '/drugs/',
          headers: {
            'Authorization': 'Token ' + this.$store.getters.getInfo.token
          },
          method: 'post',
          data: this.formData
        }).then(
          response => {
            if (response.data.response == 200) {
              this.$message({
                showClose: true,
                message: '创建成功',
                type: 'success'
              })
            }
          }
        ).catch(error => {
          console.log(error.response)
          if (error.response.status == 401) {
            this.$message({
              message: error.response.data.detail,
              type: 'warning'
            })
            this.$router.push({
              path: '/login'
            });
          } else if (error.response.status == 400) {
            this.$message({
              message: '创建失败',
              type: 'error'
            })
          } else {
            this.$message({
              message: '未知错误',
              type: 'error'
            })
          }
          console.log(error);
        });
      }
    },
    mounted() {
      this.adminId = this.$store.getters.getInfo.id
      this.adminToken = this.$store.getters.getInfo.token
      this.$http({
        url: '/drugs/',
        // url: '/drugs/'+adminId +'/',
        method: 'get',
        headers: {
          'Authorization': 'Token ' + this.adminToken
        },
        data: {
          page: 1,
          form: '',
          enterprise_id: this.adminId,
          search: '',
          ordering: ''
        }
      }).then(
        response => {
          //success
          console.log(response)
          this.tableData = response.data.results
        }
      ).catch(error => {
          console.log(error.response)
          if (error.response.status == 401) {
            this.$message({
              message: error.response.data.detail,
              type: 'warning'
            })
            this.$router.push({
              path: '/login'
            });
          } else if (error.response.status == 400) {
            this.$message({
              message: 'error400',
              type: 'error'
            })
          } else {
            this.$message({
              message: '未知错误',
              type: 'error'
            })
          }
          console.log(error);
        });
    },
  }

</script>

<style scoped>
  #Company_drugs_add>>>.el-form {
    max-width: 800px;
    margin: 20px auto;
  }

  #Company_drugs_add>>>.el-form-item {
    /* width: 400px; */
    display: inline-block;
    margin: 10px auto;
  }

  #Company_drugs_add>>>.el-form-item__content {
    width: 220px;
  }
  /* #Company_medcode >>> .el-dialog input{
    width: 250px;
} */

</style>
